package com.api.shopapi.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import com.api.shopapi.entity.Menu;
import com.api.shopapi.entity.MyResult;
import com.api.shopapi.entity.PreCheck;
import com.api.shopapi.entity.dto.MenuDTO;
import com.api.shopapi.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

//菜单

@RestController
@RequestMapping("Menu")
public class MenuController extends BaseController{


    @Autowired
    @Qualifier("MenuServiceImplOne")
    private MenuService menuService;







    //列表
    @RequestMapping(value = "List",method = RequestMethod.POST)
    public MyResult List(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

//            if(!checkSignPC(timeStr,sign)){
//                return failed("签名错误");
//            }
            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Menu/List");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }


            List<Menu> listMenu = menuService.getListByParentMenuCode(emptyUUID);
            List<MenuDTO> listMenuDTO = new ArrayList<MenuDTO>();
            //mapper.map(listMenu,listMenuDTO);
            for (Menu menu:listMenu) {
                MenuDTO menuDTO = mapper.map(menu, MenuDTO.class);
                listMenuDTO.add(menuDTO);
            }

            for (MenuDTO menuDTO:listMenuDTO) {
                List<Menu> listMenu2 = menuService.getListByParentMenuCode(menuDTO.getMenuCode());
                List<MenuDTO> listMenuDTO2 = new ArrayList<MenuDTO>();
                for (Menu menu:listMenu2) {
                    MenuDTO menuDTO2 = mapper.map(menu, MenuDTO.class);
                    listMenuDTO2.add(menuDTO2);
                }

                for (MenuDTO menuDTO2:listMenuDTO2) {
                    List<Menu> listMenu3 = menuService.getListByParentMenuCode(menuDTO2.getMenuCode());
                    List<MenuDTO> listMenuDTO3 = new ArrayList<MenuDTO>();
                    for (Menu menu:listMenu3) {
                        MenuDTO menuDTO3 = mapper.map(menu, MenuDTO.class);
                        listMenuDTO3.add(menuDTO3);
                    }

                    menuDTO2.setListMenuDTO(listMenuDTO3);
                }

                menuDTO.setListMenuDTO(listMenuDTO2);
            }


            return success(listMenuDTO);

        }catch (Exception e){
            insertError("/Menu/List", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }







    //检查本级中是否存在该菜单
    Boolean exist(JSONArray arr, String menuCode) {
        for (int k = 0; k < arr.size(); k++) {
            JSONObject menuDy = arr.getJSONObject(k);
            if (menuDy == null) {
                continue;
            }
            if (menuDy.getString("menuCode").equals(menuCode)) {
                return true;
            }
        }
        return false;
    }


    //添加菜单
    void AddMenu(String parentCode,JSONArray arr) {


        //删除不存在的菜单
        List<Menu> listMenuTemp = menuService.getListByParentMenuCode(parentCode);
        for (Menu menu: listMenuTemp) {
            if(!exist(arr,menu.getMenuCode())){
                menuService.deleteByMenuCode(menu.getMenuCode());
            }
        }


        for (int k = 0; k < arr.size(); k++) {
            JSONObject menuDy = arr.getJSONObject(k);
            Menu menu = menuService.getByMenuCode(menuDy.getString("menuCode"));
            if (menu == null) {
                menu = new Menu();
                menu.setMenuCode(menuDy.getString("menuCode"));
                menu.setParentMenuCode(menuDy.getString("parentMenuCode"));
                menu.setName(menuDy.getString("name"));
                menu.setUrl(menuDy.getString("url"));
                menu.setIcon(menuDy.getString("icon"));
                menu.setSort(menuDy.getInteger("sort"));
                menuService.insert(menu);
            } else {
                menu.setParentMenuCode(menuDy.getString("parentMenuCode"));
                menu.setName(menuDy.getString("name"));
                menu.setUrl(menuDy.getString("url"));
                menu.setIcon(menuDy.getString("icon"));
                menu.setSort(menuDy.getInteger("sort"));
                menuService.updateByMenuCode(menu);
            }


            //添加子级菜单
            JSONArray listMenuDy = menuDy.getJSONArray("listMenu");
            if (listMenuDy != null) {
                AddMenu(menuDy.getString("menuCode"),listMenuDy);
            }

        }

    }


    //编辑全部的菜单
    @RequestMapping(value = "Edit",method = RequestMethod.POST)
    public MyResult Edit(@RequestBody JSONObject jsonObject) {

        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            JSONArray listMenu = jsonObject.getJSONArray("listMenu");


//            if(!checkSignPC(timeStr,sign)){
//                return failed("签名错误");
//            }
            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Menu/Edit");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }


            if (listMenu != null) {
                AddMenu(emptyUUID,listMenu);
            }


            Object data = new Object[] { };
            return success(data);

        }
        catch (Exception e) {
            insertError("/Menu/Edit", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }






}
